﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MyMusicPlay._4._0
{
    public partial class MainWindow : Window
    {
        private void MessageBoxBack_MoverUp(object sender, MouseButtonEventArgs e)
        {
            MyMessageBox.Hidden();
        }

        private void MyMessageBorderIntercept(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
            return;
        }

        public void MessageBoxByUpdateMusic_Click(object sender, RoutedEventArgs e)
        {
            var key = Convert.ToInt32((sender as Button)!.Name.Split('_')[1]);
            if (Media.CurDisplayList.ContainsKey(key))
            {
                Music curMusic = Media.CurDisplayList[key];
                if (File.Exists(curMusic.Path))
                {
                    var factory = PlayApplication.GetModule<ElementFactoryBase>();
                    var boxName = factory.Get<TextBox>("UpdateMusicBox_Name")[0] as TextBox;
                    var boxSinger = factory.Get<TextBox>("UpdateMusicBox_Singer")[0] as TextBox;

                    var oldPath = curMusic.Path;
                    var index = oldPath.LastIndexOf('\\');
                    var index01 = oldPath.LastIndexOf('.');
                    var newPath = oldPath.Substring(0, index + 1) + $"{boxName.Text} - {boxSinger.Text}" + oldPath.Substring(index01, oldPath.Length - index01);
                    File.Move(oldPath, newPath);
                    Log.Write("成功");
                    MyMessageBox.Hidden();

                    curMusic.SetName(boxName.Text);
                    curMusic.SetSinger(boxSinger.Text);
                    curMusic.SetPath(newPath);

                    //刷新目前的
                    MusicPlaying.FlushedSelectMusic(curMusic);
                }
                else
                {
                    //文件不存在
                }
            }
        }
    }
}
